package br.com.consultsystems.persistencia.impl.cadastro;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import br.com.consultsystems.dominio.cadastro.Imovel;
import br.com.consultsystems.persistencia.spec.AbstractAdministradoraRepository;
import br.com.consultsystems.persistencia.spec.cadastro.ImovelPersistencia;

@Stateless(name = "imovelPersistencia")
public class ImovelPersistenciaImpl extends
		AbstractAdministradoraRepository<Imovel> implements
		ImovelPersistencia {

	protected ImovelPersistenciaImpl() {
		super(Imovel.class);
	}

	@Override
	public List<Imovel> retrieveAll() {
		return retrieveByCriteria(null);
	}

	@Override
	public List<Imovel> retrieveByNumero(String numero) throws Exception {
		
		final EntityManager em = getEntityManager();
		final CriteriaBuilder criteriaBuilder = createCriteriaBuilder();
		final CriteriaQuery<Imovel> criteriaQuery = createCriteriaQuery(criteriaBuilder);
		final Root<Imovel> root = createRoot(criteriaQuery);

		/* Repository -> Apenas pesquias as informações no banco de dados
		 * Filtro por codigo */
		final Path<String> pathNumero = root.get("numero");
		final Path<String> pathCondominioNome = root.get("condominio").get("nome");
		final Predicate predicateNumero = criteriaBuilder.like(pathNumero, "%"+numero+"%");

		criteriaQuery.where(predicateNumero);
		criteriaQuery.orderBy(criteriaBuilder.asc(pathCondominioNome), criteriaBuilder.asc(pathNumero));
		
		final TypedQuery<Imovel> typedQuery = em.createQuery(criteriaQuery);
		
		return typedQuery.getResultList();
	}

	@Override
	public List<Imovel> retrieveByNomeCondominio(String nomeCondominio) throws Exception {
		
		final EntityManager em = getEntityManager();
		final CriteriaBuilder criteriaBuilder = createCriteriaBuilder();
		final CriteriaQuery<Imovel> criteriaQuery = createCriteriaQuery(criteriaBuilder);
		final Root<Imovel> root = createRoot(criteriaQuery);

		/* Repository -> Apenas pesquias as informações no banco de dados
		 * Filtro por codigo */
		final Path<String> pathNumero = root.get("numero");
		final Path<String> pathCondominioNome = root.get("condominio").get("nome");
		final Predicate predicateCondominioNome = criteriaBuilder.like(pathCondominioNome, "%"+nomeCondominio+"%");
		
		criteriaQuery.where(predicateCondominioNome);
		criteriaQuery.orderBy(criteriaBuilder.asc(pathCondominioNome), criteriaBuilder.asc(pathNumero));
		
		final TypedQuery<Imovel> typedQuery = em.createQuery(criteriaQuery);
		
		return typedQuery.getResultList();
	}

	@Override
	public List<Imovel> retrieveByIdCondominio(Long idCondominio) {

		final EntityManager em = getEntityManager();
		final CriteriaBuilder criteriaBuilder = createCriteriaBuilder();
		final CriteriaQuery<Imovel> criteriaQuery = createCriteriaQuery(criteriaBuilder);
		final Root<Imovel> root = createRoot(criteriaQuery);

		/* Repository -> Apenas pesquias as informações no banco de dados
		 * Filtro por codigo */
		final Path<Long> pathIdCondominio = root.get("condominio").get("id");
		final Predicate predicateIdCondominio = criteriaBuilder.equal(pathIdCondominio, idCondominio);
		
		criteriaQuery.where(predicateIdCondominio);
		
		final TypedQuery<Imovel> typedQuery = em.createQuery(criteriaQuery);
		return typedQuery.getResultList();
	}
	
}
